﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;

namespace WJP.WPF.Controls.Toolkit
{
    /// <summary>
    /// 消息框控件
    /// </summary>
    public class MessageBox : WindowControl
    {
        static MessageBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(MessageBox), new FrameworkPropertyMetadata(typeof(MessageBox)));
        }

        public MessageBox()
        {
            Visibility = Visibility.Collapsed;
        }

        #region 属性/字段

        private MessageBoxButton _button = MessageBoxButton.OK;
        private MessageBoxResult _defaultResult = MessageBoxResult.None;
        private Window _owner;

        /// <summary>
        /// 消息框结果
        /// </summary>
        public MessageBoxResult MessageBoxResult { get; private set; } = MessageBoxResult.None;

        /// <summary>
        /// 父容器
        /// </summary>
        protected Window Container
        {
            get { return Parent as Window; }
        }

        #region 图片源 ImageSource
        /// <summary>
        /// 图片源
        /// </summary>
        public ImageSource ImageSource
        {
            get { return (ImageSource)GetValue(ImageSourceProperty); }
            set { SetValue(ImageSourceProperty, value); }
        }

        /// <summary>
        /// 图片源
        /// </summary>
        public static readonly DependencyProperty ImageSourceProperty =
            DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(MessageBox), new UIPropertyMetadata(null));
        #endregion

        #endregion

        #region Show

        public static MessageBoxResult Show(string messageText)
        {
            return Show(messageText, string.Empty, MessageBoxButton.OK, null);
        }

        public static MessageBoxResult Show(Window owner, string messageText)
        {
            return Show(owner, messageText, string.Empty, MessageBoxButton.OK, null);
        }

        public static MessageBoxResult Show(string messageText, string caption)
        {
            return Show(messageText, caption, MessageBoxButton.OK, null);
        }

        public static MessageBoxResult Show(Window owner, string messageText, string caption)
        {
            return Show(owner, messageText, caption, null);
        }

        public static MessageBoxResult Show(Window owner, string messageText, string caption, Style messageBoxStyle)
        {
            return Show(owner, messageText, caption, MessageBoxButton.OK, messageBoxStyle);
        }

        public static MessageBoxResult Show(string messageText, string caption, MessageBoxButton button)
        {
            return Show(messageText, caption, button, null);
        }

        public static MessageBoxResult Show(string messageText, string caption, MessageBoxButton button, Style messageBoxStyle)
        {
            return ShowCore(null, messageText, caption, button, MessageBoxImage.None, MessageBoxResult.None, messageBoxStyle);
        }

        public static MessageBoxResult Show(Window owner, string messageText, string caption, MessageBoxButton button)
        {
            return Show(owner, messageText, caption, button, null);
        }

        public static MessageBoxResult Show(Window owner, string messageText, string caption, MessageBoxButton button, Style messageBoxStyle)
        {
            return ShowCore(owner, messageText, caption, button, MessageBoxImage.None, MessageBoxResult.None, messageBoxStyle);
        }

        public static MessageBoxResult Show(string messageText, string caption, MessageBoxButton button, MessageBoxImage icon)
        {
            return Show(messageText, caption, button, icon, null);
        }

        public static MessageBoxResult Show(string messageText, string caption, MessageBoxButton button, MessageBoxImage icon, Style messageBoxStyle)
        {
            return ShowCore(null, messageText, caption, button, icon, MessageBoxResult.None, messageBoxStyle);
        }

        public static MessageBoxResult Show(Window owner, string messageText, string caption, MessageBoxButton button, MessageBoxImage icon)
        {
            return Show(owner, messageText, caption, button, icon, null);
        }

        public static MessageBoxResult Show(Window owner, string messageText, string caption, MessageBoxButton button, MessageBoxImage icon, Style messageBoxStyle)
        {
            return ShowCore(owner, messageText, caption, button, icon, MessageBoxResult.None, messageBoxStyle);
        }

        public static MessageBoxResult Show(string messageText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult)
        {
            return Show(messageText, caption, button, icon, defaultResult, null);
        }

        public static MessageBoxResult Show(string messageText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult, Style messageBoxStyle)
        {
            return ShowCore(null, messageText, caption, button, icon, defaultResult, messageBoxStyle);
        }

        public static MessageBoxResult Show(Window owner, string msg, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult)
        {
            return Show(owner, msg, caption, button, icon, defaultResult, null);
        }

        public static MessageBoxResult Show(Window owner, string msg, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult, Style messageBoxStyle)
        {
            return ShowCore(owner, msg, caption, button, icon, defaultResult, messageBoxStyle);
        }

        private static MessageBoxResult ShowCore(Window owner, string msg, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult, Style boxStyle)
        {
            MessageBox messageBox = new MessageBox();
            messageBox.InitializeMessageBox(owner, msg, caption, button, icon, defaultResult);
            if (boxStyle != null)
                messageBox.Style = boxStyle;
            //messageBox.ShowDialog();
            return messageBox.MessageBoxResult;
        }

        #endregion

        #region ShowDialog

        //public bool? ShowDialog()
        //{
        //    if (Parent != null)
        //        throw new InvalidOperationException("");
        //    MessageBoxResult = MessageBoxResult.None;
        //    Visibility = Visibility.Visible;
        //    CreateContainer();
        //    return Container.ShowDialog();
        //}

        private Window CreateContainer()
        {
            Window window = new Window()
            {
                AllowsTransparency = true,
                Background = Brushes.Transparent,
                Topmost = true,
                Content = this,
            };
            window.WindowStartupLocation = window.Owner == null ?
                WindowStartupLocation.CenterScreen :
                WindowStartupLocation.CenterOwner;
            window.ShowInTaskbar = false;
            window.SizeToContent = SizeToContent.WidthAndHeight;
            window.ResizeMode = ResizeMode.NoResize;
            window.WindowStyle = WindowStyle.None;
            window.Closed += new EventHandler(OnContainerClosed);

            return window;
        }

        private void OnContainerClosed(object sender, EventArgs e)
        {
            Container.Closed -= new EventHandler(OnContainerClosed);
            Container.Content = null;
            Debug.Assert(Container == null);
            OnClose();
        }

        private void OnClose()
        {
            Visibility = Visibility.Collapsed;
            OnClosed(EventArgs.Empty);
        }

        /// <summary>
        /// 消息框已关闭事件
        /// </summary>
        public event EventHandler Closed;

        protected virtual void OnClosed(EventArgs e)
        {
            Closed?.Invoke(this, e);
        }

        #endregion

        protected void InitializeMessageBox(Window owner, string msg, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult)
        {
            Text = msg;
            Caption = caption;
            _button = button;
            _defaultResult = defaultResult;
            if (!BrowserInteropHelper.IsBrowserHosted)
                _owner = owner;
            SetImageSource(icon);
        }

        /// <summary>
        /// 设置消息框图标
        /// </summary>
        /// <param name="image"></param>
        protected virtual void SetImageSource(MessageBoxImage image)
        {

        }
    }
}
